package cc.wsyw126.java.nowcoder.interview;

import cc.wsyw126.java.lintCode.swap_two_nodes_in_linked_list.ListNode;

/**
 * Created by Administrator on 2017/4/24.
 * 输入一个链表，输出该链表中倒数第k个结点。
 */
public class FindKthToTail {
    public ListNode FindKthToTail(ListNode head, int k) {
        if (head == null) return head;
        int position =k;
        ListNode node = head;
        while (k>0 && node!= null) {
            k--;
            node=node.next;
        }
        if (k>0)return null;
        ListNode result = head;
        while (node!=null) {
            node=node.next;
            result = result.next;
        }
        return result;
    }
}
